using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public abstract partial class PagedPage :BasePage
{
    #region fields
    protected int pageSize = 10;
    protected int totals;
    protected int currentPage;
    protected string p;

    #endregion

    #region properties
    /// <summary>
    /// this property should have syntax like this: [aspx page]?page=
    /// </summary>
    public abstract string RequestString
    {
        get;
    }
    #endregion

    #region methods
    /// <summary>
    /// Check if a string have valid pattern for a number
    /// </summary>
    /// <param name="strToCheck"></param>
    /// <returns></returns>
    protected bool IsNumeric(string strToCheck)
    {
        if (strToCheck == null || strToCheck == "")
            return false;
        return Regex.IsMatch(strToCheck, "^\\d+$");
    }
    /// <summary>
    /// Get page number from request
    /// </summary>
    /// <param name="p"></param>
    /// <returns></returns>
    protected int GetPageNumber(string p)
    {
        int result;
        if (p == "")
            result = 1;
        if (!IsNumeric(p))
            result = 1;
        else
            result = int.Parse(p);
        return result;
    }
    /// <summary>
    /// method to get paging bar
    /// </summary>
    /// <param name="totalRecords"></param>
    /// <param name="iPageSize"></param>
    /// <param name="iCurrentPageIndex"></param>
    /// <returns></returns>
    protected string GetPageGrid(int totalRecords, int iPageSize, int iCurrentPageIndex)
    {
        int totalPages = 0;
        totalPages = totalRecords / pageSize;
        if (totalRecords % pageSize != 0)
        {
            totalPages += 1;
        }
        // bao nhieu chi so phan trang
        const int QUICK_PAGE = 5;
        int i = 0;
        //string str = null;
        string str = "Page:";
        int iCurrentPage = 0;
        int iPageCount = 0;
        int iSpace = 0;
        //int iQuickPage = 0;
        int iStart = 0;
        int iEnd = 0;

        iPageCount = (int)(totalRecords / iPageSize);
        if (totalRecords % iPageSize > 0)
        {
            iPageCount += 1;
        }

        iCurrentPage = iCurrentPageIndex;
        //iQuickPage = QUICK_PAGE;
        if (iCurrentPage % QUICK_PAGE > 0)
        {
            iSpace = (int)(iCurrentPage / QUICK_PAGE) + 1;
        }
        else
        {
            iSpace = (int)(iCurrentPage / QUICK_PAGE);
        }
        if (iPageCount < iSpace * QUICK_PAGE)
        {
            iEnd = (iSpace * QUICK_PAGE) - ((iSpace * QUICK_PAGE) - iPageCount);
        }
        else
        {
            iEnd = (iSpace * QUICK_PAGE);
        }

        if (iEnd < QUICK_PAGE)
        {
            iStart = 1;
        }
        else
        {
            iStart = (iEnd - QUICK_PAGE) + 1;
        }

        if (iCurrentPage > QUICK_PAGE)
        {
            if (iStart - 1 == iCurrentPage)
            {
                str = str + "...";
            }
            else
            {
                str += " " + "<a href = '" + RequestString + (iStart - 1) + "'>" + "Prev" + "</a>";
            }
        }
        for (i = iStart; i <= iEnd; i++)
        {
            if (i == iCurrentPage)
            {
                str += " " + i;
            }
            else
            {
                str += " " + "<a href = '" + RequestString + i + "'>" + i + "</a>";
            }
        }
        if (iCurrentPage < iPageCount & iEnd < iPageCount)
        {
            if (iCurrentPage == iEnd + 1)
            {
                str = " " + str + "...";
            }
            else
            {
                str += " " + "<a" + " href = '" + RequestString + (iEnd + 1) + "'>" + "Next" + "</a>";
            }
        }
        str += "<span style='margin-left:10px;'> of " + totalPages.ToString() + " pages</span>";
        return str;
    }
    #endregion
}
